package com.zjs.practice.service.impl;

import com.zjs.practice.mapper.MysqlPracticeMapper;
import com.zjs.practice.service.ThreadPoolPracticeService;
import com.zjs.practice.domain.AffairTest;
import com.zjs.practice.domain.Message;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 线程池练习
 * @author zhu
 * @version 1.0
 * @date 2021/1/30 14:35
 */
@Slf4j
@Service
@Transactional
public class ThreadPoolPracticeServiceImpl implements ThreadPoolPracticeService {


    @Resource
    private MysqlPracticeMapper mapper;

    @Resource
    private ThreadPoolExecutor poolExecutor;

    @Override
    public Message testThreadPoolExecutor() {
        String orderNo = String.valueOf(System.currentTimeMillis());
        log.info("订单号：{}", orderNo);
        for (int i = 0; i < 30; i++) {
            poolExecutor.execute(() -> {
                AffairTest affairTest = new AffairTest();
                affairTest.setOrderNo(orderNo);
                affairTest.setStock(80);
                affairTest.setUpdatePerson("创建者");
                mapper.insert(affairTest);
                System.out.println(1111);
            });
        }
        return Message.ok();
    }

}
